package com.example.dllo.keepdemo.train.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RatingBar;
import android.widget.TextView;

import com.example.dllo.keepdemo.R;
import com.example.dllo.keepdemo.greendao.TrainWorkouts;
import com.example.dllo.keepdemo.train.bean.TrainFragmentBean;

import java.util.List;

/**
 * Created by dllo on 16/1/12.
 */
public class WorkoutsListViewAdapter extends BaseAdapter {
    private Context context;
    private List<TrainFragmentBean.DataEntity.WorkoutsEntity> workoutsEntityList;
    private List<TrainWorkouts> workouts;
    //test = 1判断为没网状态
    private int test;

    public WorkoutsListViewAdapter(Context context,List<TrainFragmentBean.DataEntity.WorkoutsEntity> workoutsEntityList,int hasNet) {
        this.context = context;
        this.workoutsEntityList = workoutsEntityList;
        this.test = hasNet;
    }

    public WorkoutsListViewAdapter(List<TrainWorkouts> workouts,Context context,int noneNet) {
        this.context = context;
        this.workouts = workouts;
        this.test = noneNet;
    }

    @Override
    public int getCount() {
        if (test == 1) {
            return workouts.size();
        } else {
            return workoutsEntityList.size();
        }
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_listview_fragment_train_workouts,parent,false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (test == 1) {
            viewHolder.tvTitle.setText(workouts.get(position).getTitle());
            viewHolder.tvDifficulty.setText("K" + workouts.get(position).getDifficulty());
            switch (Integer.parseInt(workouts.get(position).getDifficulty())) {
                case 1:
                    viewHolder.tvFoundations.setText("零基础");
                    break;
                case 2:
                    viewHolder.tvFoundations.setText("初学");
                    break;
                case 3:
                    viewHolder.tvFoundations.setText("进阶");
                    break;
                case 4:
                    viewHolder.tvFoundations.setText("强化");
                    break;
                case 5:
                    viewHolder.tvFoundations.setText("挑战");
                    break;
            }
            viewHolder.tvTime.setText(workouts.get(position).getTime() + "分钟");
            viewHolder.tvBody.setText(workouts.get(position).getBody());
            viewHolder.tvJoin.setText(workouts.get(position).getJoin() + "人已参加");
            viewHolder.ratingBar.setRating(Float.parseFloat(workouts.get(position).getDifficulty()));
            viewHolder.tvEquipments.setText(workouts.get(position).getEquipments());
        } else {
            viewHolder.tvTitle.setText(workoutsEntityList.get(position).getName());
            viewHolder.tvDifficulty.setText("K" + workoutsEntityList.get(position).getDifficulty());
            switch (workoutsEntityList.get(position).getDifficulty()) {
                case 1:
                    viewHolder.tvFoundations.setText("零基础");
                    break;
                case 2:
                    viewHolder.tvFoundations.setText("初学");
                    break;
                case 3:
                    viewHolder.tvFoundations.setText("进阶");
                    break;
                case 4:
                    viewHolder.tvFoundations.setText("强化");
                    break;
                case 5:
                    viewHolder.tvFoundations.setText("挑战");
                    break;
            }
            viewHolder.tvTime.setText(workoutsEntityList.get(position).getDuration() + "分钟");
            String content = "";
            for (int i = 0; i < workoutsEntityList.get(position).getTrainingpoints().size(); i++) {
                content = workoutsEntityList.get(position).getTrainingpoints().get(i).getName() + " ";
            }
            viewHolder.tvBody.setText(content);
            viewHolder.tvJoin.setText(workoutsEntityList.get(position).getPioneer() + "人已参加");
            viewHolder.ratingBar.setRating((float) workoutsEntityList.get(position).getDifficulty());
            viewHolder.tvEquipments.setText(workoutsEntityList.get(position).getEquipments().get(0).getName());
        }



        return convertView;
    }

    class ViewHolder {
        private TextView tvTitle,tvDifficulty,tvFoundations,tvEquipments,tvTime,tvBody,tvJoin;
        private RatingBar ratingBar;

        public ViewHolder(View view) {
            tvTitle = (TextView) view.findViewById(R.id.tv_item_train_workouts_title);
            tvDifficulty = (TextView) view.findViewById(R.id.tv_item_train_workouts_difficulty);
            tvFoundations = (TextView) view.findViewById(R.id.tv_item_train_workouts_foundations);
            tvEquipments = (TextView) view.findViewById(R.id.tv_item_train_workouts_equipments);
            tvTime = (TextView) view.findViewById(R.id.tv_item_train_workouts_time);
            tvBody = (TextView) view.findViewById(R.id.tv_item_train_workouts_whole_body);
            tvJoin = (TextView) view.findViewById(R.id.tv_item_train_workouts_join_person_number);
            ratingBar = (RatingBar) view.findViewById(R.id.rating_item_train_workouts_star);
        }
    }
}
